home *** CD-ROM | disk | FTP | other *** search
/ Leonardo the Inventor / Leonardo The Inventor (93026)(Broderbund)(Riverdeep)(2004).iso / LEOWINMV / SHARED.DIR / 03072_Script_BINARY SEARCH < prev    next >
Text File  |  1996-04-01  |  2KB  |  58 lines

  1. -- -----------------------------------------------------------
  2. -- Handler binSearchFirstItemInLine performs a binary search
  3. -- searching for the line in given source containing the given
  4. -- target. the source has the given delimiter.
  5.  
  6. on BinSearchFirstItemInLine source, target, delimiter
  7.   -- Source must be sorted, and have a blank line at the end.
  8.   
  9.   if (length(source) = 0) or (length(target) = 0) then return 0
  10.   
  11.   set oldDelimiter = the itemDelimiter
  12.   
  13.   if not(voidP(delimiter)) then
  14.     set the itemDelimiter = delimiter
  15.   end if
  16.   
  17.   set RangeBegin = 1
  18.   set RangeEnd = the number of lines of source
  19.   set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
  20.   
  21.   set found = FALSE
  22.   
  23.   repeat while found = FALSE 
  24.     set maybeTarget = item 1 of line CurrentLine of Source
  25.    
  26.     -- in case the line begins with a ":"
  27.     if maybeTarget = EMPTY then
  28.       set currentline = currentline - 1
  29.     else if maybeTarget = Target then
  30.       set found = TRUE
  31.     else if (RangeEnd - RangeBegin = 1) then
  32.       if item 1 of line RangeEnd of source = target then
  33.         set found = TRUE
  34.         set CurrentLine = rangeEnd
  35.       else if item 1 of line RangeBegin of source = target then
  36.         set found = TRUE
  37.         set CurrentLine = RangeBegin
  38.       else
  39.         exit repeat
  40.       end if
  41.     else if (RangeEnd - RangeBegin = 1) then
  42.       set CurrentLine = RangeEnd
  43.     else if maybeTarget < Target then
  44.       set RangeBegin = CurrentLine
  45.       set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
  46.     else -- maybeTarget > Target
  47.       set RangeEnd = CurrentLine
  48.       set CurrentLine = RangeBegin + integer ((RangeEnd - RangeBegin) / 2)
  49.     end if
  50.   end repeat
  51.   
  52.   set the itemDelimiter = oldDelimiter
  53.   if found then
  54.     return CurrentLine
  55.   else 
  56.     return 0
  57.   end if
  58. end